package ar.edu.itba.pod.legajo48240.event.threads;

import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;

import ar.edu.itba.event.EventInformation;
import ar.edu.itba.pod.legajo48240.model.Host;
import ar.edu.itba.pod.thread.CleanableThread;

public class EventProcessThread extends CleanableThread{

	BlockingQueue<EventInformation> processed;
	
	private final Host localhost;
	
	private final static Logger LOGGER = Logger.getLogger(EventProcessThread.class);
	
	public EventProcessThread(String name, BlockingQueue<EventInformation> processed, Host localhost) {
		super(name);
		this.processed=processed;
		this.localhost=localhost;
		
	}
	
	@Override
	public void run() {
		super.run();
			try {
				while(!shouldFinish()){
					Thread.sleep(10000*1000);
					LOGGER.info(localhost.node().id() + " se acabaron 10 segundos?");
					if(!processed.isEmpty()){
						int total=processed.size();
						LOGGER.info( localhost.node().id() + " CLEANING PROCESSED EVENT QUEUE "+ processed.size() + " \tOF " + localhost.id());
						for( int i=0;i<(total*0.7+1);i++){
							processed.poll();
						}
					}else{
						LOGGER.info(localhost.node().id() + " proceseed is empty !");
					}
				}
			} catch (InterruptedException e) {
				LOGGER.info("finishing queue process thread... ");
			}
		
	}

}



